package com.pj;

import org.springframework.stereotype.Component;

/**
 * @author: szy
 * @program: pj-novel
 * @description: 线程工具类
 * @create: 2022-10-21 11:28
 **/
@Component
public class ThreadUtils {
    public static Thread findThread(long threadId) {
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        while(group != null) {
            Thread[] threads = new Thread[(int)(group.activeCount() * 1.2)];
            int count = group.enumerate(threads, true);
            for(int i = 0; i < count; i++) {
                if(threadId == threads[i].getId()) {
                    return threads[i];
                }
            }
            group = group.getParent();
        }
        return null;
    }
}
